package com.koron.css2.projectCenter.impl;

import com.koron.css2.ApplicationConfig;
import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.BaseAccessoryMetadataBean;
import com.koron.css2.baseConfig.bean.BaseReceiptAccessoryBean;
import com.koron.css2.baseConfig.mapper.BaseAccessoryMetadataMapper;
import com.koron.css2.baseConfig.mapper.BaseReceiptAccessoryMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.*;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;

/**
 * pdf文件生成
 *
 * @author: zhongxj
 * @date: 2020-07-22 16:35
 **/

public class PDFFileUpload implements ServerInterface {


    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        String path = "";
        try {
            ArrayList<String> list = JsonUtils.objectToPojo(req.getData(), ArrayList.class);
            // 获取模板路径
            String templatePath = this.getClass().getClassLoader().getResource(PDFUtils.tempPath).getPath();
            //  二维码路径
            String prCodePath = this.getClass().getClassLoader().getResource(PDFUtils.prCodePath).getPath();
            // 第二个数据存放订单编号，用于生成二维码
            String bill = list.get(1);
            //生成二维码
            if(!StringUtils.trim(bill).equals("")){
                QRCodeUtil.encode(bill, null, prCodePath, true);
            }else{
                throw new RuntimeException("单据编号不能为空，无法生成二维码！");
            }
            // 文件已经生成在pdf文件夹中了
            PDFUtils.editPDFField(templatePath,list,prCodePath);
            // 获取生成的pdf路径
            String outPath = this.getClass().getClassLoader().getResource(PDFUtils.outPath).getPath();
            File outFile = new File(outPath);
            InputStream inputStream = new FileInputStream(outFile);
            MultipartFile file = new MockMultipartFile(outFile.getName(),outFile.getName(),"application/pdf",inputStream);
            path = Upload.upload(file);
            if (StringUtils.isNotBlank(path)) {
                BaseAccessoryMetadataMapper metadataMapper = factory.getMapper(BaseAccessoryMetadataMapper.class);
                BaseReceiptAccessoryMapper mapper = factory.getMapper(BaseReceiptAccessoryMapper.class);
                BaseAccessoryMetadataBean metadata = new BaseAccessoryMetadataBean();
                String metadataId = Tools.getObjectId();
                metadata.setAccessoryPath(path);
                metadata.setAccessoryName(FilenameUtils.getName(file.getOriginalFilename()));
                metadata.setId(metadataId);
                metadata.setAccessorySize(file.getSize());
                metadata.setAccessoryType(file.getContentType());
                metadata.setCreateAccount(userInfo.getUserInfo().getAcount());
                metadata.setCreateName(userInfo.getUserInfo().getName());
                metadata.setSourceFlag("1");
                metadataMapper.insert(metadata);
                BaseReceiptAccessoryBean bean = new BaseReceiptAccessoryBean();
                bean.setId(Tools.getObjectId());
                bean.setMetadataId(metadataId);
                bean.setCreateAccount(userInfo.getUserInfo().getAcount());
                bean.setCreateName(userInfo.getUserInfo().getName());
                bean.setAccessoryName(FilenameUtils.getName(file.getOriginalFilename()));
                mapper.insert(bean);
            }
            // 上传完成把输出的pdf删除
            if(outFile.exists()){
                outFile.delete();
            }
        } catch (Exception e) {
            logger.error("上传pdf文件失败" + e.getMessage(), e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "上传失败", null);
        }
        MessageBean<String> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "上传成功", String.class);
        msg.setData(ApplicationConfig.getAccessoryUploadUrl()+"fileDownload?path="+path);
        return msg;
    }
}
